#include "Sock.hpp"
#include "Protocol.hpp"

void Usage(string args)
{
    cout << "Usage : " << args << " server_ip server_port" << endl;
}

int main(int argc, char* argv[])
{
    if(argc != 3)
    {
        Usage(argv[0]);
        exit(-1);
    }

    int sock = Sock::Socket();
    uint16_t port = atoi(argv[2]);

    int data1, data2;
    char op;

    cout << "Please Enter FirstData# ";
    cin >> data1;
    cout << "Please Enter SecondData# ";
    cin >> data2;
    cout << "Please Enter Operator# ";
    cin >> op;

    request_t rq{data1, data2, op};
    string json_rq(SerializeRequest(rq));
    Sock::Connect(sock, argv[1], port);

    ssize_t sz = write(sock, json_rq.c_str(), json_rq.size());
    if(sz > 0)
    {
        response_t rp;
    #define NUM 1024
        char buf[NUM] = {0};
        read(sock, buf, sizeof(buf) - 1);
        DeserializeResponse(buf, rp);

        cout << "code[0:success] : " << rp.code << " result : " << rp.result << endl; 
    }
    return 0;
}